<?php
/**
* @copyright (C) 2003 - 2008 SystN International Pty Ltd.
* @license http://www.systn.com
* SystN is Free Software
*/

Class Dirver{
	//连接数据库
	function DBLink($dba=''){
		$this->LinkID = ($dba['pconnect'] == 1)? @mysql_pconnect($dba['dbhost'], $dba['dbuser'], $dba['dbpass']) : @mysql_connect($dba['dbhost'], $dba['dbuser'], $dba['dbpass']);
		if (!$this->LinkID){ return 1; }
		
		//选择连接数据库
		if (!@mysql_select_db($dba['dbname'], $this->LinkID)){ return 2; }
		
		//字符集处理
		if($this->version() > '4.1') {
			$charset = (in_array(strtolower($dba['charset']), array('gbk', 'big5', 'utf-8')))?$dba['charset']:'utf-8';
			$this->query("SET NAMES '".str_replace('-', '', $dba['charset'])."'");
		}
		return $this->LinkID;
	}
	
	//查询语句
	function query($query,$limit='') {
		$this -> nums ++;
		//检测如果有限制数据集则处理
		if($limit>0){
			$query = $query.' LIMIT '.$limit;
		}
		//时间处理
		if (eregi("now()",$query)){
			$query = preg_replace("/(.*)\'now\(\)\'(.*)/is","\\1now()\\2",$query);
		}
		$this-> Lists	.= $query.'&nbsp;&nbsp;<font>'.date('Y-m-d H:i:s')."</font><br>";
		
		$this->result_id = @mysql_query($query,$this->LinkID);
		
		return $this->result_id;
	}
	
	//返回数组资料
	function fetch_array($query) {
		return @mysql_fetch_array($query, MYSQL_ASSOC);
	}
	
	//取得返回列的数目
	function num_rows($query){
		return @mysql_num_rows($query);
	}
	
	//返回单列的各字段
	function fetch_row($query) {
		return @mysql_fetch_row($query);
	}
	
	//返回最后一次使用 INSERT 指令的 ID
	function insert_id(){
		return @mysql_insert_id($this->LinkID);
	}
	
	//关闭当前数据库连接
	function close(){
		return @mysql_close($this->LinkID);
	}
	
	//检测mysql版本
	function version(){
		$query = @mysql_query("SELECT VERSION()",$this->LinkID);
		return	@mysql_result($query, 0);
	}
	
	//查询语句数
	function sql_num(){
		return $this -> nums;
	}
	
	//查询语句列表
	function sql_debug(){
		return $this-> Lists;
	}
	
	
}
?>